import os

import allure
from openpyxl import load_workbook
from test_pom.test4_八爪云接口自动化测试.base_api.base_api import BaseApi


class OrderApi(BaseApi):
    base_url = "http://api.wxorder.taover.com"

    @allure.step("文本录入订单")
    def word_create_order(self, data):
        word_url = f"{self.base_url}/v1/wxorderorder"
        req = {
            "method": "post",
            "url": word_url,
            "json": data,
            "headers": {
                "Authorization": self.get_token()
            }
        }
        res = self.send_api(req)
        return res

    @allure.step("上传excel文件")
    def upload_excel(self, file_path):
        file_url = f"{self.base_url}/v1/wxorderorder/file"
        # params = {
        #     "file": load_workbook(with open("data.xlsx", "rb") as f: f)
        # }
        # req = {
        #     "url": file_url,
        #     "method": "post",
        #     "files": params,
        #     "headers": {
        #         "Authorization": self.get_token()
        #     }
        # }
        with open(file_path, "r") as f:
            params = {
                "file": (  # 上传文件的元组格式：(文件名, 文件对象, MIME类型)
                    os.path.basename(file_path),  # 文件名
                    f,  # 文件对象
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"  # Excel的MIME类型
                )
            }
            req = {
                "url": file_url,
                "method": "post",
                "files": params,
                "headers": {
                    "Authorization": self.get_token()
                }
            }
        res = self.send_api(req)
        return res

    @allure.step("创建文件订单")
    def file_create_order(self, data):
        file_url = f"{self.base_url}/v1/excelorder/9757/channelGoodsNameReflect"
        req = {
            "method": "post",
            "url": file_url,
            "json": data,
            "headers": {
                "Authorization": self.get_token()
            }
        }
        res = self.send_api(req)
        return res

    @allure.step("根据收件人姓名和手机号查询订单")
    def search_order(self, data):
        search_url = f"{self.base_url}/v1/wxorderorder"
        req = {
            "method": "get",
            "url": search_url,
            "params": data,
            "headers": {
                "Authorization": self.get_token()
            }
        }
        res = self.send_api(req)
        return res

    @allure.step("修改订单金额")
    def update_order_price(self, data):
        update_url = f"{self.base_url}/v1/wxorderorder/money"
        req = {
            "method": "put",
            "url": update_url,
            "json": data,
            "headers": {
                "Authorization": self.get_token()
            }
        }
        res = self.send_api(req)
        return res


if __name__ == '__main__':
    print(OrderApi().upload_excel(r"C:\Users\29435\Downloads\导入数据.xlsx"))
